import C from "html2canvas";
import M from "jspdf";
import { chunk as y } from "lodash-unified";
const m = {
  "h-v": "h-v",
  //  打印顺序: 先横向, 再垂直
  h: "h",
  //  仅横向
  v: "v",
  //  仅垂直 [默认]
  "v-h": "v-h"
  //  先垂直, 再横向
}, f = {
  portrait: [595.28, 841.89],
  // 纵向比
  landscape: [841.89, 595.28]
  // 横线比
}, x = {
  fileName: "文件.pdf",
  orientation: "portrait",
  multiMode: m.v,
  scale: 1,
  chunkSize: 10
};
function S(a) {
  if (!a.fileName)
    return "fileName";
  if (!a.chunkSize)
    return "chunkSize";
  if (!a.multiMode)
    return "multiMode";
  if (!a.orientation)
    return "orientation";
  if (!a.scale)
    return "scale";
}
function p(a, o, w, l, n) {
  const e = { x: 0, y: 0 }, { docImage: r, contentSize: u, unExportSize: t, pageSize: s } = w;
  if (o === m.v) {
    const i = l * 1, c = i / u.w * u.h;
    for (console.log("contentSize: ", u), console.log("page: ", s), console.log("imgW, imgH, a4W, a4H: ", i, c, l, n); Math.floor(t.h) > 0; )
      a.addImage(r, "JPEG", e.x, e.y, i, c), console.log("unExport.h: ", t.h), console.log("exportOffset: ", e), t.h = t.h - s.h / 1, e.y = e.y - n, Math.floor(t.h) > 0 && a.addPage();
    console.log("unExport.h: ", t.h);
  }
  if (o === m.h) {
    const i = l * 1, c = i / u.w * u.h;
    for (console.log("contentSize: ", u), console.log("page: ", s), console.log("imgW, imgH, a4W, a4H: ", i, c, l, n); Math.floor(t.h) > 0; )
      a.addImage(r, "JPEG", e.x, e.y, i, c), console.log("unExport.h: ", t.h), t.h = t.h - s.h / 1, e.y = e.y - n, Math.floor(t.h) > 0 && a.addPage();
    console.log("unExport.h: ", t.h);
  }
  if (o === m["h-v"]) {
    const c = l * 2, d = c / u.w * u.h;
    console.log("imgW, imgH, a4W, a4H: ", c, d, l, n), console.log("page: ", s);
    for (let v = 0; v < 3; v++)
      for (let g = 0; g < 2; g++)
        a.addImage(
          r,
          "JPEG",
          s.w / 2,
          s.h / 3,
          l,
          d
        ), a.addPage();
    console.log("unExport.h: ", t.h);
  }
}
async function b(a, o = x, w = (...l) => {
}) {
  o = { ...x, ...o };
  const l = S(o);
  if (l) {
    console.error(`PDF导出失败：${l} 值不正确`, o);
    return;
  }
  const [n, e] = f[o.orientation], r = new M({
    orientation: o.orientation,
    unit: "pt",
    format: "a4"
  }), u = await C(a, {
    scale: o.scale,
    allowTaint: !0
  }), t = a.getBoundingClientRect(), s = {
    w: t.width * o.scale,
    h: t.height * o.scale
  }, h = u.toDataURL("image/jpeg", 1), i = {
    contentSize: s,
    unExportSize: {
      w: s.w,
      h: s.h
    },
    pageSize: {
      w: s.h / e * n,
      h: s.w / n * e
    },
    docImage: h
  };
  switch (o.multiMode) {
    case m.v:
      p(r, o.multiMode, i, n, e);
      break;
    case m.h:
      p(r, o.multiMode, i, n, e);
      break;
    case m["v-h"]:
      p(r, o.multiMode, i, n, e);
      break;
    case m["h-v"]:
      p(r, o.multiMode, i, n, e);
      break;
  }
  if (r.save(o.fileName), typeof w == "function") {
    const c = setTimeout(() => {
      w(), c && clearTimeout(c);
    }, 300);
  }
}
async function H(a, o = x, w = (...l) => {
}) {
  o = { ...x, ...o };
  const l = S(o);
  if (l) {
    console.error(`PDF导出失败：${l} 值不正确`, o);
    return;
  }
  const [n, e] = f[o.orientation], r = new M({
    orientation: o.orientation,
    unit: "pt",
    format: "a4"
  }), u = y(a, o.chunkSize);
  for (const [t, s] of u.entries()) {
    const h = document.createElement("div");
    h.style.position = "absolute", h.style.left = "-9999px", s.forEach((z) => {
      h.append(z.cloneNode(!0));
    }), document.body.append(h);
    const i = await C(h, {
      scale: o.scale,
      allowTaint: !0
    }), c = h.getBoundingClientRect(), d = {
      w: c.width * o.scale,
      h: c.height * o.scale
    }, v = i.toDataURL("image/jpeg", 1), g = {
      contentSize: d,
      unExportSize: {
        w: d.w,
        h: d.h
      },
      pageSize: {
        w: d.h / e * n,
        h: d.w / n * e
      },
      docImage: v
    };
    switch (o.multiMode) {
      case m.v:
        p(r, o.multiMode, g, n, e);
        break;
      case m.h:
        p(r, o.multiMode, g, n, e);
        break;
      case m["v-h"]:
        p(r, o.multiMode, g, n, e);
        break;
      case m["h-v"]:
        p(r, o.multiMode, g, n, e);
        break;
    }
    t !== u.length - 1 && r.addPage(), document.body.removeChild(h);
  }
  if (r.save(o.fileName), typeof w == "function") {
    const t = setTimeout(() => {
      w(), t && clearTimeout(t);
    }, 300);
  }
}
export {
  H as pdfExportMulti,
  b as pdfExportSingle
};
